package com.buss.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.product.dao.CatePropGroupDao;
import com.buss.product.model.CatePropGroup;
import com.buss.product.model.GoodsCategory;
import com.buss.product.service.CatePropGroupService;
import com.buss.product.service.GoodsCategoryService;

@Service
public class CatePropGroupServiceImpl implements CatePropGroupService {

	@Autowired
	private CatePropGroupDao propGroupDao;
	
	@Autowired
	private GoodsCategoryService categoryService;
	
	@Override
	public CatePropGroup savePropGroup(CatePropGroup propGroup) {
		return propGroupDao.savePropGroup(propGroup);
	}

	@Override
	public void editPropGroup(CatePropGroup propGroup) {
		propGroupDao.editPropGroup(propGroup);
	}

	@Override
	public void removePropGroup(Long[] ids) {
		propGroupDao.removePropGroup(ids);
	}

	@Override
	public CatePropGroup loadPropGroup(Long id) {
		return propGroupDao.loadPropGroup(id);
	}

	@Override
	public ResultList<CatePropGroup> queryCatePropGroup(String companyId,
			String name, RecordStatus status, OrderBy orderBy, Paginalbe page) {
		return propGroupDao.queryCatePropGroup(companyId, name, status, orderBy, page);
	}

	@Override
	public ResultList<CatePropGroup> queryCatePropGroupByCategoryId(
			String companyId, Long categoryId, RecordStatus status,
			OrderBy orderBy, Paginalbe page) {		
		return propGroupDao.queryCatePropGroupByCategoryId(companyId, categoryId, status, orderBy, page);
	}

	@Override
	public ResultList<CatePropGroup> queryCatePropGroupByParentCategoryId(
			String companyId,Long categoryId, RecordStatus status, OrderBy orderBy,
			Paginalbe page) {
		List<GoodsCategory> categorys = categoryService.queryCategoryByChildId(categoryId);
		Long[] categoryIds = new Long[categorys.size() + 1];
		for(int i = 0;i < categorys.size();i++){
			categoryIds[i] = categorys.get(i).getId();
		}
		categoryIds[categoryIds.length - 1] = 0L;
		
		ResultList<CatePropGroup> propGroups = propGroupDao.queryCatePropGroupByParentCategoryId(companyId, categoryIds, status, orderBy, page);
		return propGroups;
	}

	public ResultList<CatePropGroup> queryCatePropGroupNotByParentCategoryId(String companyId,Long categoryId,RecordStatus status,OrderBy orderBy,Paginalbe page){
		List<GoodsCategory> categorys = categoryService.queryCategoryByChildId(categoryId);
		Long[] categoryIds = new Long[categorys.size() + 1];
		for(int i = 0;i < categorys.size();i++){
			categoryIds[i] = categorys.get(i).getId();
		}
		categoryIds[categoryIds.length - 1] = 0L;
		ResultList<CatePropGroup> propGroups = propGroupDao.queryCatePropGroupNotByParentCategoryId(companyId, categoryIds, status, orderBy, page);
		return propGroups;
	}
	
}
